macroScript KeensBatchExporter_Bones category:"File"
(
--///////////////////////////////////////////////////
--This MaxScript started out as a simple test that
--would allow you to export objects using their pivot
--as the scene origin(so if you had 5 objects 
--scattered around the scene it would move them to 
--0,0,0 for export then move them back after export is 
--done).I got a request for batch operations in the
--script and thought that would be a nice feature so
--i did some research on that and now the script allows
--you to batch export to any format that 3ds max 
--supports and use the scene object name as the file 
--name. You can choose to export only selected 
--(or unselected) objects, or export all objects in 
--the scene. It also allows you to add prefixes to 
--the beginning and/or end of the file name on export,
--or add them to the scene object name.
--///////////////////////////////////////////////////

global prefixpopupgui

fn DoExport outputpath ext useobjpivot exportsetup exportsel invertsel pfxb pfxbs pfxa pfxas = 
(
	-- Creating collision box here, so it doesn't get recreated each time /PACO
	
	collision_box = box length:20 width:20 height:20
	collision_box.name = "UCX_"
			--Create bone here /Santiago	
	--main_bone = bonesys.createBone [0,0,0] [0,1,0] [0,0,1]
	if (outputpath != undefined) do
	(
		OldSelection = getCurrentSelection()
		if (not exportsel) then
			max select all
		else if (invertsel) then
			max select invert
		
		objList = getCurrentSelection()
		nameList = #()

	for obj in objList where(obj.parent == undefined) do
	(
		if(findItem nameList obj.name == 0) do
		(
			append nameList obj
		)
	)
	
	objList = nameList

		listLength = objList.count as string
		
		
		for p=1 to objList.count do
		(
		
			if (pfxb != "") then
				Prefix1 = pfxb + pfxbs
			else
				Prefix1 = ""
			if (pfxa != "") then
				Prefix2 = pfxa + pfxas
			else
				Prefix2 = ""


			OldPosition = objList[p].Position
			OldRotation = objList[p].rotation
			

			
			exportName = outputPath + "\\" + Prefix1 + objList[p].name + Prefix2 + "." + ext

			select objList[p]
			if (useobjpivot) do
			(
				OldPosition = objList[p].Position
				objList[p].Position = [0,0,0]
				objList[p].rotation.x_rotation = 0
				objList[p].rotation.y_rotation = 0
				objList[p].rotation.z_rotation = 0
				

				
				-- Setting parameters of collision_box to current selection /PACO
				collision_box.width = objList[p].max.x - objList[p].min.x
				collision_box.length = objList[p].max.y - objList[p].min.y
				collision_box.height = 20
				collision_box.position = objList[p].position
				collision_box.position.z = objlist[p].min.z - 20--align box with bottom of road piece
				
				
				
				--Activate modify mode, select the object, add Skin modifier, then add bone /PACO
				--max modify mode
				select objList[p]
			
				--Selects both collision and original before exporting begins
				select objList[p]
				if isGroupHead objList[p] do selectMore objList[p].children
				selectMore collision_box

			)
			if (exportsetup and p == 1) then
				exportfile exportName selectedOnly:TRUE
			else
				exportfile exportName #noPrompt selectedOnly:TRUE
			
			saveName = outputPath + "\\"+ objList[p].name  + Prefix2 + ".max"
			max views redraw
			--saveNodes selection saveName
			
			if (useobjpivot) then
			(
				objList[p].Position = OldPosition
				objList[p].rotation = inverse OldRotation
			)
				
			if (p == objList.count) then
				select OldSelection		--reselect old selection
			
		) --end for
	) --end if
			delete collision_box
) --end fn

fn DoMaxSave outputpath useobjpivot exportsel invertsel pfxb pfxbs pfxa pfxas = 
(
	if (outputpath != undefined) do
	(
		OldSelection = getCurrentSelection()
		if (not exportsel) then
			max select all
		else if (invertsel) then
			max select invert
		
		objList = getCurrentSelection()
		
		for p=1 to objList.count do
		(
			if (pfxb != "") then
				Prefix1 = pfxb + pfxbs
			else
				Prefix1 = ""
			if (pfxa != "") then
				Prefix2 = pfxas + pfxa
			else
				Prefix2 = ""
				
			saveName = outputPath + "\\" + Prefix1 + objList[p].name + Prefix2 + ".max"
			select objList[p]
			if (useobjpivot) do
			(
				OldPosition = $.Position
				$.position = [0,0,0]
			)
				max views redraw
				saveNodes selection saveName
			
			if (useobjpivot) then
				$.Position = OldPosition
			if (p == objList.count) then
				select OldSelection		--reselect old selection
				
		) --end for
	) --end if
) --end fn

fn DoPrefixToObject pfxb pfxbs pfxa pfxas =
(
		objList = getCurrentSelection()
		
		for p=1 to objList.count do
		(
				Prefix1 = pfxb + pfxbs
				Prefix2 = pfxas + pfxa
				
			ChangeNameTo = Prefix1 + objList[p].name + Prefix2
			select objList[p]
			
				$.name = ChangeNameTo
			
		) --end for
)

--Start GUI

rollout prefixpopup "Add Prefix To Object Name" width:446 height:130
(
	--Not being used anymore.
	GroupBox grp48 "Prefix Warning" pos:[2,6] width:252 height:120
	label lbl164 "WARNING! You are about to add one or more Prefixes to the the names of all currently selected Objects. Are you sure you want to continue?" pos:[8,24] width:240 height:45
	button clickyes "Yes" pos:[96,88] width:64 height:24
	
	edittext newpfxb "" pos:[262,45] width:125 height:20
	GroupBox grp96 "AddPrefix" pos:[259,7] width:184 height:111
	edittext newpfxbs "" pos:[388,45] width:48 height:20
	label lbl308 "Prefix before name" pos:[265,29] width:100 height:16
	label lbl309 "Separator" pos:[387,29] width:48 height:16
	edittext newpfxa "" pos:[262,90] width:125 height:20
	edittext newpfxas "" pos:[388,90] width:48 height:20
	label lbl310 "Prefix after name" pos:[265,74] width:83 height:16
	label lbl311 "Separator" pos:[387,74] width:48 height:16
	
	on clickyes pressed do
	(
		DoPrefixToObject newpfxb.text newpfxbs.text newpfxa.text newpfxas.text
		--closeRolloutFloater prefixpopupgui
	)
	
	on clickno pressed do
	(
		closeRolloutFloater prefixpopupgui
	)
)

rollout base "Batch Export" width:188 height:480
(
	
	label lbl1 "Output Directory" pos:[4,32] width:105 height:16
	edittext outputpath "" pos:[0,48] width:156 height:20
	label lbl2 "Extension (ase=good .ase=bad)" pos:[4,80] width:159 height:16
	edittext editExt "" pos:[0,96] width:36 height:20
	button kExport "EXPORT" pos:[12,128] width:60 height:26
	checkbox useobjpivot "Checkbox" pos:[8,178] width:16 height:16 checked:true
	GroupBox grp1 "Output" pos:[2,8] width:184 height:113
	GroupBox grp2 "Modify" pos:[2,158] width:184 height:129
	label lbl12 "Use Object Pivot As Origin" pos:[24,178] width:129 height:16
	button DirLookUp "..." pos:[160,48] width:16 height:20
	checkbox exportsetup "Checkbox" pos:[8,198] width:16 height:16 checked:true
	label lbl25 "Configure Export Settings" pos:[24,198] width:129 height:16
	
	edittext pfxb "" pos:[5,355] width:125 height:20
	GroupBox grp15 "AddPrefix" pos:[2,317] width:184 height:111
	edittext pfxbs "" pos:[131,355] width:48 height:20
	label lbl40 "Prefix before name" pos:[8,339] width:100 height:16
	label lbl41 "Separator" pos:[130,339] width:48 height:16
	edittext pfxa "" pos:[5,400] width:125 height:20
	edittext pfxas "" pos:[131,400] width:48 height:20
	label lbl78 "Prefix after name" pos:[8,384] width:83 height:16
	label lbl79 "Separator" pos:[130,384] width:48 height:16
	
	checkbox exportsel "Checkbox" pos:[8,218] width:16 height:16 checked:true
	label lbl128 "Export Only Selected Meshes" pos:[24,218] width:147 height:16 enabled:true
	
	checkbox invertsel "Checkbox" pos:[8,238] width:16 height:16 checked:false
	label lbl129 "Invert Selection" pos:[24,238] width:129 height:16
	
	checkbox savemax "Checkbox" pos:[40,98] width:16 height:16 enabled:true checked:false
	label lbl193 "Save As .MAX file" pos:[56,98] width:90 height:16
	button savemaxbutton "Save .MAX files" pos:[88,128] width:80 height:26 enabled:false
	
	button prefix2object "Asign prefixes to object names" pos:[16,293] width:156 height:20
		on  base open do
	(
			outputpath.text = "C:\\UDK\\RawContent\\roads_only"
			editExt.text = "fbx"
	)
	
	on kExport pressed do
	(
		if (outputpath.text == "" and editExt.text == "") then
			messagebox("Please specify an output directory and a file extension.")
		else if (outputpath.text == "") then
			messagebox("Please specify an output directory.")
		else if (editExt.text == "") then
			messagebox("Please specify a file extension.")
		else if (not doesFileExist outputpath.text) then
			messagebox("'" + outputpath.text + "'" + " does not exist, please enter a valid output directory.")
		else
			DoExport outputpath.text editExt.text useobjpivot.state exportsetup.state exportsel.state invertsel.state pfxb.text pfxbs.text pfxa.text pfxas.text
	)
	on DirLookUp pressed do
	(
		outputpath.text = getSavePath "Save to:"
	)
	on exportsel changed state do
	(
		if (exportsel.checked == true) then
		(
			invertsel.enabled = true
		)
		else
		(
			invertsel.enabled = false
			invertsel.checked = false
		)
	)
	on savemax changed state do
	(
		if (savemax.checked == true) then
		(
			editExt.enabled = false
			savemaxbutton.enabled = true
			kExport.enabled = false
		)
		else
		(
			editExt.enabled = true
			savemaxbutton.enabled = false
			kExport.enabled = true
		)
	)
	on savemaxbutton pressed do
	(
		if (outputpath.text == "") then
			messagebox("Please specify an output directory.")
		else if (not doesFileExist outputpath.text) then
			messagebox("'" + outputpath.text + "'" + " does not exist, please enter a valid output directory.")
		else
			DoMaxSave outputpath.text useobjpivot.state exportsel.state invertsel.state pfxb.text pfxbs.text pfxa.text pfxas.text
	)

	on prefix2object pressed do
	(
		if ($ == undefined) then
			messagebox("Please select objects to add prefixes to.")
		else if (subobjectLevel != 0) then
			messagebox("Please get out of subobject mode.")
		else
		(
			--prefixpopupgui = newRolloutFloater "Prefix to Object" 454 164
			--addRollout prefixpopup prefixpopupgui
			DoPrefixToObject pfxb.text pfxbs.text pfxa.text pfxas.text
		)
	)

)


rollout info "info/contact" width:188 height:430
(
	GroupBox grp48 "Contact" pos:[2,6] width:184 height:79
	label lbl160 "authors:" pos:[8,21] width:160 height:16
	label lbl161 "Kurt'commander_keen'Loeffler" pos:[8,35] width:160 height:16
	label lbl162 "email:" pos:[9,49] width:160 height:16
	label lbl163 "kurtloeffler@gmail.com" pos:[9,63] width:162 height:16
	GroupBox grp53 "Info" pos:[2,97] width:184 height:306
	label lbl164 "This MaxScript started out as a simple test that would allow you to export objects using their pivot as the scene origin(so if you had 5 objects scattered around the scene it would move them to 0,0,0 for export then move them back after export is done).I got a request for batch operations in the script and thought that would be a nice feature so i did some research on that and now the script allows you to batch export to any format that 3ds max supports and use the scene object name as the file name. You can choose to export only selected (or unselected) objects, or export all objects in the scene. It also allows you to add prefixes to the beginning and/or end of the file name on export, or add them to the scene object name." pos:[8,112] width:170 height:289
)

gui = newRolloutFloater "BatchExport" 192 480
addRollout base gui
addRollout info gui

)